<?php

namespace app\admin\api;

use app\admin\model\SiteModel;

class SiteApi
{
    /**
     * 站点列表 用于模板设计
     * @param array $param
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function index($param = [])
    {
        $data      = [];
        $siteWhere = [];
        $siteModel = new SiteModel();

        // pre($param,1);

        //数据源地址(规则标识)
        if( !empty($param['rule_name']) ){
            $roleId = session('ROLE_ID');
            //若不是超级管理员，查询角色拥有该规则标识的站点列表
            if( $roleId != 1 ){
                $where = [
                    'rule_name' => $param['rule_name'],
                    'role_id'   => session('ROLE_ID'),
                ];
                $siteIds = db('auth_access')->where($where)->column('site_id');
                $siteWhere['site_id'] = ['IN', $siteIds];
            }

            $data = $siteModel
                 -> where($siteWhere)
                 -> order('site_id')
                 -> select()
                 -> toArray();
            foreach ($data as &$v) {
                $v['id'] = $v['site_id'];
            }
        }

        return $data;
    }

}
